"""
【解释器】
解释器模式(Interpreter Pattern)
定义了一个语言的文法，且建立一个解释器来解释该语言中的句子。这个模式常用于需对一些特定语言进行处理或解析的场景。
"""
from abc import ABC, abstractmethod


class Expression(ABC):
    @abstractmethod
    def interpret(self):
        pass


class NumberExpression(Expression):
    def __init__(self, number):
        self.number = number

    def interpret(self):
        return self.number


class PlusExpression(Expression):
    def __init__(self, expression1, expression2):
        self.expression1 = expression1
        self.expression2 = expression2

    def interpret(self):
        return (self.expression1.interpret() +
                self.expression2.interpret())


class MinusExpression(Expression):
    def __init__(self, expression1, expression2):
        self.expression1 = expression1
        self.expression2 = expression2

    def interpret(self):
        return (self.expression1.interpret() -
                self.expression2.interpret())


expression = MinusExpression(
    PlusExpression(NumberExpression(5),
                   NumberExpression(3)),
    NumberExpression(2)
)

print(expression.interpret())  # 6
